

function set_order(obj){ 
var model=$(obj).attr("table");
var key=$(obj).attr("key");
var key_value=$(obj).attr("id");
var value=$(obj).val();
$.post("/BoxAdmin/Index/set_order",{model:model,key:key,key_value:key_value,value:value},function(data){
    //if(data!=1)alert(data);
  });
}


 function extend(target, options) {
        for (name in options) {
            target[name] = options[name];
        }
        return target;
    }

 /******播放mp3***/
function play_mp3(mp3){
	//console.log("开始");
	hiApp.showIndicator();
	loadAudio(mp3,function(){
		if($("#mp3div").length>0){
			$("#mp3div").remove();
		}
		var html="<div id='mp3div' style='display:none'>";
		html+='<audio id="music" src="'+mp3+'" preload="auto" controls >';
		html+="</div>";
		$("body").append(html);
		audio = $("#music")[0];;
		audio.play();
		hiApp.hideIndicator();	 
		//console.log("方向");
	})
	
}

function loadAudio(src, callback) {
	var audio = new Audio(src);
	audio.onloadedmetadata = callback;
	audio.src = src;

}
 

/****退出登陆**/
function logout(obj){
	$.getJSON("/BoxApi/BoxSNS/Member/logout",function(data){
		$("body").append(data.msg);
		var callback=$(obj).attr("callback");
		if(typeof(callback)!="undefined"){
			eval(callback);
		}
	});
}


function after_logout(){
	 window.location.href="/";
}




/*******弹出层***/
function dialog(content,callback,autohide){
	if(typeof(autohide) == "undefined"){
		autohide=2000;
	}
	$(".dialog").remove();
	$("body").append('<div class="dialog alert alert-danger"></div>');
	if(content){
		$(".dialog").html(content);
		var w="-"+$(".dialog").width()/2+"px";
		$(".dialog").css({"top":'70%',"left":"50%","margin-left":w});
		 re = setTimeout(function(){
								 $(".dialog").fadeOut(500);
								   if(typeof(callback) == "function") {
														callback();
									   } 
								  },autohide);
		
	  
	} 
}
 


/******加载下一页*****************/
function ajaxNextUrlPage(obj){
	   var next_input=$(obj).next("input");
	   var nextUrl=$(next_input).val();//下一页URL
	   $(next_input).val("");//下一页为空以免重复加载
	   if(nextUrl=="" || typeof(nextUrl) == "undefined"){
		    dialog("没有更多了！！");
			$('.ajax_loading').hide()
			$("#pullDown").hide();
		   return false;//没有一下页了
	   }
	   hiApp.get(nextUrl, function(ret){
		   	   var html=$(ret).find(".after_div").html();
			   var NownextUrl=$(ret).find("#nextUrl").val();	
			   $(obj).parent().prev(".after_div").append(html); //将ajxa请求的数据追加到内容里面
			   $(next_input).val(NownextUrl);
			   hiApp.bind($(obj).parent().prev(".after_div"));
	   });
}


/*********加载页面*******/
function load_page(url,source,to_id,act,func){
	if(typeof(act) == "undefined"){
		act="append";
	}
	$.getJSON(url,function(data){
		if(data.data==null){
			return ;	
		}
		 var html=box_render(data.data.list,source,to_id,act);
		 if(data.nextUrl!=""){
		  $(to_id).next(".pagerTemplate").html($("#pagerTemplate").html());
		  $(to_id).next(".pagerTemplate").find("#nextUrl").val(data.nextUrl);
		  $(to_id).next(".pagerTemplate").find("#nextUrl").attr("source",source);
		 }
		 if(typeof(func) == "function"){
			 func();
		 }
	})
}

/******渲染数据****/
function box_render(data,source,to_id,act){
	var source_html=$(source).html();
	//console.log(source_html);
	Render_data={'items':data};
	var render = template.compile(source_html);
	var compiledRendered = render(Render_data);
	if(typeof(to_id) == "undefined"){
		return compiledRendered;
	}else if($(to_id).length>0){
		if(act=="append"){
			$(to_id).append(compiledRendered);	
		}else if(act=="html"){
			$(to_id).html(compiledRendered);	
		}else if(act=="prepend"){
			$(to_id).prepend(compiledRendered);	
		}
		hiApp.bind($(to_id));
	}
}

/******获得url参数****/
function getQueryString(name) { 
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
	var r = window.location.search.substr(1).match(reg); 
	if (r != null) return unescape(r[2]); return null; 
} 

function getQuery(viewURL){
	if(viewURL.indexOf("#") >= 0){
	    urls = viewURL.split("#");
		if(urls[1]!="" && urls[1].indexOf("/") >= 0){
			url=urls[1];	
		}else{
			url=viewURL;
		}
	}else{
		url=viewURL;
	}
	
	var param={};
	//console.log("getQuery:"+url);
	param=GetRequest2(url);
	//console.log(param);
	if(url.indexOf("?") >= 0){
		var params = GetRequest(url);
		$.each(params,function(i,v){param[i]=v});
	}
	return param;
}

function GetRequest2(url){
	url=trim(url,"/");
	var theRequest = new Object();
	if (url.indexOf("?") != -1) {
		var str = url.split("?"); 
		str=str[0];
	}else{
		str=url;
	}
	
	if (str.indexOf("/") != -1) { 
		strs=str.split("/");
		var strs_array=new Array();
		if(url.indexOf("http://")>-1 && url.indexOf("https://")){
			for(var i = 3; i < strs.length; i ++) { 
				strs_array.push(strs[i]);
			}
		}else{
			strs_array=strs;	
		}
		//console.log(strs_array);
		for(var i = 0; i < strs_array.length; i ++) { 
			if(i%2==0 && strs_array[i]){
				theRequest[strs_array[i]]=strs_array[i+1];
			}
		}
		
	}
	return theRequest; 
}

function GetRequest(url) {  
	var theRequest = new Object(); 
	if (url.indexOf("?") != -1) { 
		var str = url.split("?"); 
		str=str[1];
		strs = str.split("&"); 
		for(var i = 0; i < strs.length; i ++) { 
			theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]); 
		} 
	} 
	return theRequest; 
} 
/******获得url参数结束****/

function trim(cstring,s){
	//console.log(cstring.substring(0));
	if(cstring.substr(0,1)==s)
	{
	//字符串以"开头，去掉"
	cstring=cstring.substr(1,cstring.length-1)
	}
	if(cstring.substr(-1)==s)
	{
	//字符串以"结尾，去掉"
	cstring=cstring.substr(0,cstring.length-2)
	}	
	return cstring;
}


 

var is_paly={};//播放的视频
var subtitle_list={};
function loadedHandler(id,s){
		if(id){
			var play_id="ckplayer_"+id;
			var video=CKobject.getObjectById(play_id);
			is_paly[play_id]=0;
			if(video.getType()){//说明使用html5播放器
				video.addListener('play',playHandler(play_id,s));
				//video.addListener('pause',pauseHandler(play_id));
				//video.addListener('time',timeHandler);
				var curl_video=$("#"+play_id)[0];
				curl_video.ontimeupdate = function () { 
						timeHandler(play_id);
				};
			}
			else{
				video.addListener('play','playHandler("'+play_id+'",'+s+')');
				//video.addListener('pause','pauseHandler("'+play_id+'")');
				video.addListener('time','timeHandler("'+play_id+'")');
				
			}
		}
}
/***播放器初始化***/
function playHandler(play,s){
	$("#videoWrapper").trigger("videoPlay")
	// console.log("play:"+play);
	if( is_paly[play]==0 && s>0){
		 is_paly[play]=1;
		 playerVideoSeek(play,s);
	}
	//console.log("12321321");
	//console.log(play);
	/*$("#"+play).parent().bind(hiApp.device.click,function(){
			var play=$(this).find("video").attr("id");
			CKobject.getObjectById(play).playOrPause();
			event.preventDefault();
			event.stopPropagation();
	})*/
//	var data=CKobject.getObjectById(play).getStatus();
//	var time=data['time'];
//	var start_time=$("#words_content_wrapper .swiper-slide-active .english:first").attr("start_time_int");
//	var end_time=$("#words_content_wrapper .swiper-slide-active .english:last").attr("start_time_int");
//console.log(start_time);
//	var d=end_time-start_time;

	/*if(time>start_time && time<end_time){
		CKobject.getObjectById(play).videoPlay();
	}else{
		playerVideoSeek(play,start_time);
		CKobject.getObjectById(player).videoPlay();
	}*/
	
}
/*播放第几秒**/
function playerVideoSeek(player,s){
	CKobject.getObjectById(player).videoSeek(s);
	CKobject.getObjectById(player).videoPlay()

}
/*****暂停****/
function pauseHandler(player){
	console.log("pauseHandler:"+player);
}

/*时间监听**/
function timeHandler(play_id){
	var data=CKobject.getObjectById(play_id).getStatus();
	var time=data['time'];
	subtitle(play_id,time);
	swiper_to_page(time);
}

/*******播放视频***/
function open_video(param){
				var url=param['url'];
				var flashvars={
								f:url,
								c:0,
								p:2,
								b:0,
								h:1,
								i:param['image'],
				//subtitle_en:param['subtitle_en'],
				//loaded:"loadedHandler()",
				};
				if(param['subtitle_html5']){
				flashvars['subtitle_html5']	= param['subtitle_html5'];
				}
				if(param['loaded']){
				flashvars['loaded']	= param['loaded'];
				}
				//console.log(flashvars);
				var params={bgcolor:'#FFF',allowFullScreen:false,allowScriptAccess:'always',wmode:'transparent'};
				var video=[url+'->video/mp4'];
				var h=$("#"+param['id']).height();
				var w=$("#"+param['id']).width();
				CKobject.embed('/Public/Js/ckplayer6.6/ckplayer/ckplayer.swf',param['id'],"ckplayer_"+param['id'],w,h,true,flashvars,video,params);
	
}


/****字幕啊**/
function subtitle(id,time){
	//console.log(subtitle_list);
	//console.log(subtitle_list[id]);
	//console.log(time);
	//console.log(id);
	var obj=$("#"+id).parent();
	var words="";
	var bool=0;
	if(!subtitle_list[id]){
		return ;	
	}
	$.each(subtitle_list[id],function(i,v){
		 var end_time_int=v.end_time_int;	
		 var start_time_int=v.start_time_int;
		 if(start_time_int<time && time<end_time_int){
			 words=v.words;
			 bool=1;
			 return false;
		 }	
	})
	if(bool==0){
		$(obj).find(".subtitle").remove();
		return;
	}
	 
	if($(obj).find(".subtitle").length==0){
	  var subtitle='<div class="subtitle">'+words+'</div>';
			$(obj).append(subtitle);
	  }else{
			$(obj).find(".subtitle").html(words);
	 }
	
}

 

//绑定事件
function bindEvents(bindings) {
            for (var i in bindings) {
                if(bindings[i].selector) {
					$(bindings[i].element)
                        .off(bindings[i].event,bindings[i].selector , bindings[i].handler);
                    $(bindings[i].element)
                        .on(bindings[i].event,bindings[i].selector , bindings[i].handler);
                }else{
					$(bindings[i].element)
                        .unbind(bindings[i].event, bindings[i].handler);
                    $(bindings[i].element)
                        .bind(bindings[i].event, bindings[i].handler);
                }
            }
   }
   



 //当是app的时候，apiCo运行app
var run_app_interval;
function run_app(){
	run_app_interval=setInterval(function(){
		if(hiApp.hasinit==1){
	 		$(document).trigger('appRun');
			clearTimeout(run_app_interval);
		}
	},500)
}

 